package com.dooioo.pns.business.service;

import com.dooioo.pns.base.model.Pagination;
import com.dooioo.pns.base.service.BaseService;
import com.dooioo.pns.entity.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.support.service
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-04-01 下午2:00)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Service
public class MessageService extends BaseService<Message> {

	@Autowired
	private MessageReadService  messageReadService;
	/**
	 * 新增message
	 * @param message
	 * @return
	 */
	public int insertMessage(Message message){
		if(super.insertAndReturnId(sqlId("insertAndReturnId"),message) >0){
			return message.getId();
		}
		return 0;
	}

	/**
	 * 更新messageId
	 * @param id 消息id
	 * @param messageId leancloud 消息id
	 * @return
	 */
	public boolean updateObjectId(int id,String messageId){
		Message message = new Message();
		message.setId(id);
		message.setMessageId(messageId);
		return super.update("updateObjectId", message);
	}

	/**
	 * 根据消息id 获取消息详情
	 * @param messageId 消息id
	 * @return 消息实体
	 */
	public Message getBean(int messageId) {
		return  super.selectOne(sqlId("queryBeanById"), messageId);
	}

	/**
	 * 获取员工消息分页
	 * @param userCode 工号
	 * @param appId 目标应用appId
	 * @param businessId 推送方id
	 * @return 分页实体
	 */
	public Pagination getMessagePagination(int userCode,int appId,int businessId,int pageSize,int pageNo) {
		Pagination<Message> messagePagination = new Pagination<>();
		messagePagination.setPageNo(pageNo);
		messagePagination.setPageSize(pageSize);
		messagePagination.setOrder("id desc");
		messagePagination.setWhere(" userCode = " + userCode + " and appId = " + appId + " and businessId = " + businessId);
		messagePagination  = super.getPagination(sqlId("queryMessagePagination"), sqlId("queryMessageCount"), messagePagination);
		if(messagePagination.getList().size() > 0){
			messageReadService.updateStatusByAppIdAndBusinessId(userCode,appId,businessId);
		}
		return super.getPagination(sqlId("queryMessagePagination"), sqlId("queryMessageCount"), messagePagination);
	}

	public List<Message> getSendFailList() {
		return super.selectList(sqlId("querySendFailList"));
	}
}
